/*******************************************************************************
 * Copyright (c) 2006 CEA LIST.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA LIST - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.ui.dialogs;


import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.filters.OperationFilter;

/**
 * This dialog shows the tree of all owned element by taking in account a filter 
 * @author Patrick Tessier
 */
public class ElementTreeSelectionDialog extends Dialog {

	/**
	 * The parent where we look for subElements
	 */
	protected Element umlParentElement;

	/**
	 * Chosen element.
	 */
	protected Element choosenElement;

	/**
	 * The selection tree.
	 */
	protected Tree tree;

	/**title of the window**/
	protected String title="Element Selection";
	
	/**link ModelManager**/
	protected ModelManager modelManager;

	private ViewerFilter[] viewerFilters;
	/**
	 * Constructor with parent shell and Element.
	 * 
	 * @param parent the Shell.
	 * @param umlparent the uml element where we look for a children
	 * @param modelManager the ModelManager
	 * @param filters this is an array of filter see {@link ViewerFilter} or an example {@link OperationFilter}
	 * @param title title of the window
	 */
	public ElementTreeSelectionDialog(Shell parent, Element umlparent,ModelManager modelManager,ViewerFilter[] filters,String title) {
		super(parent);
		this.umlParentElement = umlparent;
		// add the resize ability to the window
		setShellStyle(SWT.RESIZE | super.getShellStyle());
		this.modelManager=modelManager;
		this.viewerFilters=filters;
		this.title=title;
	}

	/**
	 * Creates the dialog area.
	 * 
	 * @param parent composite.
	 * 
	 * @return control Control.
	 */
	protected Control createDialogArea(Composite parent){
		Composite composite = (Composite) super.createDialogArea(parent);
		tree = new Tree(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.RESIZE);
		
		//use of EMF facilities
		TreeViewer newTreeViewer = new TreeViewer(tree);
		newTreeViewer.setContentProvider(new AdapterFactoryContentProvider(modelManager.getAdapterFactory()));
		newTreeViewer.setFilters(viewerFilters);
		newTreeViewer.setLabelProvider(new AdapterFactoryLabelProvider(modelManager.getAdapterFactory()));
		newTreeViewer.setInput(modelManager.getDiResourceSet().getResourceSet());
//		newTreeViewer.setInput(modelManager.getDiResourceSet().getAllLoadedUMLRessource());
		

		tree.setLayoutData(new GridData(550, 300));

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e){
				TreeItem[] selection = tree.getSelection();
				if(selection[0].getData() instanceof Element){
					choosenElement = (Element)selection[0].getData();
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		return composite;
	}


	/**
	 * Returns the associated Element.
	 * 
	 * @return element Element
	 */
	public Element getResult(){
		return choosenElement;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(this.title);
	}

}
